package cn.bugDance.domain.award.service;

import cn.bugDance.domain.activity.model.entity.ActivityPartakeOrderEntity;
import cn.bugDance.domain.award.event.SendAwardEvent;
import cn.bugDance.domain.award.model.aggregate.UserAwardRecordAggregate;
import cn.bugDance.domain.award.model.entity.DistributeAwardEntity;
import cn.bugDance.domain.award.model.entity.TaskEntity;
import cn.bugDance.domain.award.model.entity.UserAwardRecordEntity;
import cn.bugDance.domain.award.model.valobj.SendAwardVO;
import cn.bugDance.domain.award.model.valobj.TaskStateVO;
import cn.bugDance.domain.award.model.valobj.UserAwardRecordStateVO;
import cn.bugDance.domain.award.repository.IAwardRepository;
import cn.bugDance.domain.award.service.distribute.IDistributeAward;
import cn.bugDance.domain.strategy.model.entity.RaffleAwardEntity;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


@Service
@Slf4j
public class AwardService implements IAwardService{

    @Resource
    private IAwardRepository awardRepository;

    private Map<String, IDistributeAward> distributeAwardGroup;

    public AwardService(Map<String, IDistributeAward> distributeAwardGroup){
        this.distributeAwardGroup = distributeAwardGroup;
    }


    @Override
    public void saveAwardRecord(RaffleAwardEntity raffleAwardEntity, ActivityPartakeOrderEntity activityPartakeOrderEntity) {

        if(null == raffleAwardEntity || null == activityPartakeOrderEntity){
            log.info("【奖品服务层-AwardService】: 参数校验错误");
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        Integer awardId = raffleAwardEntity.getAwardId();
        String awardTitle = raffleAwardEntity.getAwardTitle();

        UserAwardRecordAggregate userAwardRecordAggregate = new UserAwardRecordAggregate();

        UserAwardRecordEntity userAwardRecordEntity = new UserAwardRecordEntity();
        userAwardRecordEntity.setAwardId(awardId);
        userAwardRecordEntity.setAwardTitle(awardTitle);
        userAwardRecordEntity.setAwardTime(activityPartakeOrderEntity.getOrderTime());
        userAwardRecordEntity.setUserId(activityPartakeOrderEntity.getUserId());
        userAwardRecordEntity.setActivityId(activityPartakeOrderEntity.getActivityId());
        userAwardRecordEntity.setStrategyId(raffleAwardEntity.getStrategyId());
        userAwardRecordEntity.setAwardState(UserAwardRecordStateVO.NOT_COMPLETED);
        userAwardRecordEntity.setOrderId(activityPartakeOrderEntity.getOrderId());

        SendAwardVO sendAwardVO = new SendAwardVO();
        sendAwardVO.setAwardId(awardId);
        sendAwardVO.setAwardTitle(awardTitle);
        sendAwardVO.setUserId(userAwardRecordEntity.getUserId());
        sendAwardVO.setAwardKey(raffleAwardEntity.getAwardKey());
        sendAwardVO.setAwardConfig(raffleAwardEntity.getAwardConfig());
        sendAwardVO.setOrderId(activityPartakeOrderEntity.getOrderId());

        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setTopic(SendAwardEvent.topic);
        taskEntity.setState(TaskStateVO.NOT_COMPLETED);
        taskEntity.setMessage(SendAwardEvent.create(sendAwardVO));
        taskEntity.setUserId(userAwardRecordEntity.getUserId());

        userAwardRecordAggregate.setTaskEntity(taskEntity);
        userAwardRecordAggregate.setUserAwardRecordEntity(userAwardRecordEntity);

        awardRepository.saveAwardRecord(userAwardRecordAggregate);
    }

    @Override
    public void distributeAward(DistributeAwardEntity distributeAwardEntity) {
        Integer awardId = distributeAwardEntity.getAwardId();
        String userId = distributeAwardEntity.getUserId();
        String awardKey = distributeAwardEntity.getAwardKey();
        String awardConfig = distributeAwardEntity.getAwardConfig();
        String orderId = distributeAwardEntity.getOrderId();

        if(awardId == null || StringUtils.isBlank(userId) || StringUtils.isBlank(awardKey) || StringUtils.isBlank(awardConfig) || StringUtils.isBlank(orderId)){
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        IDistributeAward distributeAward = distributeAwardGroup.get(awardKey);

        if(null == distributeAward){
            return;
        }

        distributeAward.giveOutPrizes(distributeAwardEntity);
    }


}
